from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix,classification_report


digits = load_digits()

X = digits.data  # 特征数据
y = digits.target  # 标签数据

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.3, random_state=1
) # 划分训练集和测试集


model = LogisticRegression(
    multi_class='ovr',
    max_iter=1500,
    random_state=1
)

model.fit(X_train, y_train)
acc = model.score(X_test, y_test)
print(f"Accuracy: {acc}")
y_pred = model.predict(X_test)
print(y_pred[:10],y_test[:10])

print(f"{classification_report(y_true=y_test, y_pred=y_pred)}")
print(f"{confusion_matrix(y_true=y_test, y_pred=y_pred)}")


